package com.qianqian.service.utils;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;


public class AES {
    //加密
    public String encrypt(String str,String password) {
        try{
            String key = password; //密钥
            byte[] kb = key.getBytes("utf-8");
            SecretKeySpec sks = new SecretKeySpec(kb, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//算法/模式/补码方式
            cipher.init(Cipher.ENCRYPT_MODE, sks);
            byte[] eb = cipher.doFinal(str.getBytes("utf-8"));
            return new Base64().encodeToString(eb);
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //解密
    public static String decrypt(String str,String password) {
        try{
            String key = password; //密钥
            byte[] kb = key.getBytes("utf-8");
            SecretKeySpec sks = new SecretKeySpec(kb, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, sks);
            byte[] by = new Base64().decode(str);
            byte[] db = cipher.doFinal(by);
            return new String(db);
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args)  {


        String de = decrypt("nOoduKFDTKaGcJDhS9zW9tiT+mJzJ+iixv8jCR5SIpxA0UywlNZ+6AKjuAzCcjWYouEIWYenDLuJZCRT+faAridbMzKzzxhVuYg5iJVqwY1ips2j/CjmJCI9JQTUfpDeRH3RF2FxC17V1iMtTlA15kkL/F7yh3QpTV184MdHcXB3C91UIFuT5Uess2QzVRXSQAIWtYSyBztUOi//OPfWjqsFWU/Yo8joabJeJWHeofCRlbU8qtsU1aCz8RkhjfUK7Ex49SrDWs2gIqC4zSS1fAtDvITIrnqIyYBmTrCECIaQjpRqqwVI6K8R98oTL2KF","ERO@D!@25#45D%F#$DF!F$R816f*fg9@");

        System.out.println(JSONObject.parseObject(de));

    }
}
